package com.cmcc.example.face.noSdk.service;

import com.alibaba.fastjson.JSON;
import com.cmcc.example.face.common.util.HttpUtil;
import com.cmcc.example.face.noSdk.common.HttpURI;
import com.cmcc.example.face.noSdk.common.vo.response.AccessTokenResponseVO;
import com.cmcc.example.face.noSdk.common.vo.response.EcloudBaseResponseVO;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * <name>CommonService</name>
 * 公共操作
 * </p >
 *
 * @author arabira
 * @version 1.0 2020-04-01
 * @since 1.0
 */
@Slf4j
@Service
public class CommonService {


    @Value("${cmcc.ecloud.apiKey}")
    private String clientId;
    @Value("${cmcc.ecloud.secretKey}")
    private String clientSecret;
    @Value("${cmcc.ecloud.baseUrl}")
    private String baseUrl;

    /**
     * 通过app身份标识换取token
     * @return
     */
    private String getAccessToken() {
        String getAccessTokenUrl = baseUrl + HttpURI.GET_ACCESS_TOKEN +
                "?client_id=" + clientId +
                "&client_secret=" + clientSecret;
        log.info("request:{}", getAccessTokenUrl);
        String result = HttpUtil.instance().httpGet(getAccessTokenUrl);
        AccessTokenResponseVO accessTokenResponseVO = JSON.parseObject(result, AccessTokenResponseVO.class);
        log.info("获取到token:{}", accessTokenResponseVO.getAccessToken());
        return accessTokenResponseVO.getAccessToken();
    }

    /**
     * 组装访问接口url地址
     * @param uri
     * @return
     */
    public String compileUrl(String uri) {
        log.info("组装访问url地址，需要访问的uri地址为:{}", uri);
        String fullUrl = baseUrl + uri + "?access_token=" + getAccessToken();
        log.info("最终访问地址为:{}", fullUrl);
        return fullUrl;
    }

    /**
     * 使用POST方式访问接口并获取返回信息
     * @param requestObject
     * @param url
     * @param type
     * @param <R>
     * @param <T>
     * @return
     */
    public <R,T> EcloudBaseResponseVO<R> submitMessagePost(T requestObject, String url, Type type) {
        log.info("request message:{}", JSON.toJSON(requestObject));
        String response = HttpUtil.instance().httpPostJson(url, JSON.toJSONString(requestObject));
        EcloudBaseResponseVO<R> responseVO = new Gson().fromJson(response, type);
        return responseVO;
    }

    /**
     * 使用GET方式访问接口并获取返回信息
     * @param faceSetFinfUrl
     * @param fields
     * @param type
     * @param <R>
     * @return
     */
    public <R> EcloudBaseResponseVO<R> submitMessageGet(String faceSetFinfUrl, String fields, Type type) {
        String fullUrl = faceSetFinfUrl + fields;
        log.info("request url:{}", fullUrl);
        String response = HttpUtil.instance().httpGet(fullUrl);
        EcloudBaseResponseVO<R> responseVO = new Gson().fromJson(response, type);
        return responseVO;
    }

    /**
     * 将非标准时间格式转换为LocalDateTime对象
     * @param unusualTime
     * @return
     */
    public LocalDateTime unusualTimeformateToLocalDatetime(String unusualTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        LocalDateTime localDateTime = LocalDateTime.parse(unusualTime, formatter);
        return localDateTime;
    }
}